// pages/admin/admin.js
import { Throttle } from '../../utils/util.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sign: '',
    orderId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //车辆管理
  carManage: new Throttle().throttle(function () {
    wx.navigateTo({
      url: '/pages/vehicleAndManage/vehicleAndManage',
    })
  }, 1000),

  // 扫码
  scan: new Throttle().throttle(function () {
    let that = this;
    wx.showLoading({
      title: '加载中',
    })
    wx.scanCode({
      success: (res) => {
        console.log(res)
        
        if (res.result.length === 37) {
          //助力车扫码
          that.setData({
            code: res.result,
            sign: '1',
          })
          console.log(that.data.code);
          that.findOrder(res.result.slice(19))
        }
        //电动车扫码
        else if (res.result.length == 41){
          console.log(res);
          that.setData({
            code: res.result.slice(24),
            sign: '2'
          })
          if (res.result.slice(24, 28) == 'code'){
            that.findOrder(that.data.code)
          }
        }
        //旧电动车扫码
        else if (res.result.length == 17){
          that.setData({
            code: res.result,
            sign: '2'
          })
          if (res.result.slice(0, 4) == 'code'){
            that.findOrder(that.data.code)
          }
        }
        else {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '无效二维码。。。',
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();
        // common.showModal('扫描二维码', "扫描失败，请重试", false);
        wx.showModal({
          title: '提示',
          content: '扫描失败，请重新扫描！！！',
        })
      }
    })
  }, 2000),
  
  //查询车辆订单 赋值给app.globalData.orderInformation
  findOrder: function(e){
    let that = this;
    wx.request({//查询该车辆是否有订单
      url: app.globalData.baseUrl + '/order/getOrderByBike',
      method: 'post',
      data: {
        code: e,
        lqoSession: app.globalData.lqoSession,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        app.globalData.orderInformation = res.data;
        //存放管理员得到的cid
        wx.setStorage({
          key: 'adminCid',
          data: res.data.bike.cid,
        })
        wx.hideLoading();
        if(res.data.status == '101'){
          wx.showToast({
            title: '后台没有当前车辆！',
            icon: 'none'
          })
        }else{
          if (that.data.sign == '1' && !res.data.result.bool){
            wx.showModal({
              title: '提示',
              content: '该车暂无第三方数据，请换车！',
              showCancel: false,
            })
          } else {
            if (res.data.msg == '有订单！') {
              that.setData({
                orderId: res.data.order.orderId,
              })
            }
            wx.navigateTo({
              url: '/pages/administrators/administrators?&sign=' + that.data.sign + '&mark=' + res.data.sign + '&code=' + e + '&orderId=' + that.data.orderId + '&isSupre=' + res.data.isSuper,
            })
            app.globalData.valData = res.data.result;
            console.log(app.globalData.valData);
          }
        }
      },
      fail() {
        wx.showModal({
          title: '提示',
          content: '后台异常，请重试！',
        })
      },
      complete() {
        wx.hideLoading()
      }
    })
  },
})